﻿using System;
using System.Collections.Generic;
using MTC.Common.DTO;
using MTC.Web.Business.DTO;
using MTC.Business.Plate;
using Plate=MTC.Business.Plate.Plate;

namespace MTC.Web.Business
{
    public class Plates
    {
        private List<PlateEntity> _plateEntityList = new List<PlateEntity>();
        
        public List<PlateEntity> GetPlates()
        {
            List<MTC.Common.DTO.Plate> plates;
            plates = Plate.GetPlates();
            if (plates == null)
                return null;
            _plateEntityList.Clear();
            foreach (MTC.Common.DTO.Plate plate in plates)
            {
                PlateEntity entity = new PlateEntity();
                entity.PlateId = plate.PlateId;
                entity.OwnerId = plate.OwnerId;
                entity.PlateNumber = plate.PlateNumber;
                entity.PlateTypeId = plate.PlateTypeId;
                entity.PlateTypeDescription = plate.PlateTypeDescription;
                entity.OwnerName = plate.OwnerName;
                _plateEntityList.Add(entity);
            }
            return _plateEntityList;
        }

        public PlateEntity GetAPlate(int plateid )
        {
            if (plateid == 0)
                return null;
            PlateEntity entity = null;
            PlateFinancialEntity financial = null;
            MTC.Common.DTO.Plate plate = Plate.GetPlate(plateid);
            if (plate != null)
            {
                entity = new PlateEntity();
                entity.PlateId = plate.PlateId;
                entity.OwnerId = plate.OwnerId;
                entity.PlateNumber = plate.PlateNumber;
                entity.PlateTypeId = plate.PlateTypeId;
                
                financial = new PlateFinancialEntity();
                financial.Account = plate.Financial.Account;
                financial.Bank = plate.Financial.Bank;
                financial.Comment = plate.Financial.Comment;
                financial.EndDate = plate.Financial.EndDate;
                financial.GST = plate.Financial.GST;
                financial.MonthlyRate = plate.Financial.MonthlyRate;
                financial.PlateFinancialId = plate.Financial.PlateFinancialId;
                financial.PlateId = plate.Financial.PlateId;
                financial.StartDate = plate.Financial.StartDate;
                financial.WeeklyRate = plate.Financial.WeeklyRate;
                entity.Financial = financial;
            }
            return entity;
        }

        public bool UpdateAPlate(PlateEntity entity)
        {
            if (entity==null)
            {
                return false;
            }
            MTC.Common.DTO.PlateFinancial financial = new PlateFinancial();

            MTC.Common.DTO.Plate plate = new Common.DTO.Plate();

            plate.PlateId = entity.PlateId;
            plate.OwnerId = entity.OwnerId;
            plate.PlateNumber = entity.PlateNumber;
            plate.PlateTypeId = entity.PlateTypeId;

            plate.Financial = financial;

            financial.PlateId = entity.PlateId;
            financial.Account = entity.Financial.Account;
            financial.Bank = entity.Financial.Bank;
            financial.Comment = entity.Financial.Comment;
            financial.EndDate = entity.Financial.EndDate;
            financial.GST = entity.Financial.GST;
            financial.MonthlyRate = entity.Financial.MonthlyRate;
            financial.PlateFinancialId = entity.Financial.PlateFinancialId;
            financial.PlateId = entity.Financial.PlateId;
            financial.StartDate = entity.Financial.StartDate;
            financial.WeeklyRate = entity.Financial.WeeklyRate;

            return Plate.UpdatePlate(plate);


        }

        public bool DeleteAPlate(int PlateId)
        {
            if (PlateId == 0)
                return false;

            return Plate.DeletePlate(PlateId);
        }

        public int AddPlate(PlateEntity entity)
        {
            if (entity == null)
            {
                return 0;
            }
            MTC.Common.DTO.PlateFinancial financial = new PlateFinancial();
            MTC.Common.DTO.Plate plate = new Common.DTO.Plate();

            plate.OwnerId = entity.OwnerId;
            plate.PlateId = entity.PlateId;
            plate.PlateNumber = entity.PlateNumber;
            plate.PlateTypeId = entity.PlateTypeId;

            plate.Financial = financial;

            financial.PlateId = entity.PlateId;
            financial.Account = entity.Financial.Account;
            financial.Bank = entity.Financial.Bank;
            financial.Comment = entity.Financial.Comment;
            financial.EndDate = entity.Financial.EndDate;
            financial.GST = entity.Financial.GST;
            financial.MonthlyRate = entity.Financial.MonthlyRate;
            financial.PlateFinancialId = entity.Financial.PlateFinancialId;
            financial.PlateId = entity.Financial.PlateId;
            financial.StartDate = entity.Financial.StartDate;
            financial.WeeklyRate = entity.Financial.WeeklyRate;

            return Plate.AddPlate(plate);
        }
    }
}
